import Vue from "vue";
import Vuex from "vuex";

import taskTodo from "./modules/taskTodo";

Vue.use(Vuex);

let initState = () => ({
    userInf: {},
    serveEnum: {},
    resourcePermission: {},
    menuPermission: {},
    detailInf: {},
});
export default new Vuex.Store({
    state() {
        return initState();
    },
    getters: {},
    mutations: {
        CLEAR_STATE(state) {
            sessionStorage.removeItem("STATE_CACHE");
            this.replaceState(Object.assign(state, initState()));
        },
        FETCH_STATE_CACHE(state) {
            let STATE_CACHE = sessionStorage.getItem("STATE_CACHE");
            STATE_CACHE = STATE_CACHE && JSON.parse(STATE_CACHE);

            this.replaceState(Object.assign(state, STATE_CACHE));
        },
        SAVE_STATE_CACHE(state) {
            let SAVE_STATE = Object.assign({}, state);
            delete SAVE_STATE.route;
            sessionStorage.setItem("STATE_CACHE", JSON.stringify(SAVE_STATE));
        },
        updateUserInf(state, userInf) {
            state.userInf = userInf;
        },
        updateServeEnum(state, serveEnum) {
            state.serveEnum = serveEnum;
        },
        updateResourcePermission(state, resourcePermission) {
            state.resourcePermission = resourcePermission;
        },
        updateMenuPermission(state, menuPermission) {
            state.menuPermission = menuPermission;
        },
        cacheDetailInfo(state, objInf) {
            state.detailInf = objInf;
        },
    },
    actions: {},
    modules: {
        taskTodo,
    },
});
